﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;
using System.Collections;

namespace Acsys
{
    public partial class UserForm : Form, IChildForm
    {
        User m_user = new User();
        IMainForm m_mainForm;
        int sortColumn = 0;
        public UserForm(Form parent, string name)
        {
            InitializeComponent();
            listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
            InitializeButtonClick();
            this.Name = name;
            this.MdiParent = parent;
            m_mainForm = (IMainForm)parent;
            loadRecords();
            //ClearForm();
            //setEditMode(EditMode.New);
            if (listView1.Items.Count > 0) { listView1.Focus(); listView1.Items[0].Selected = true; }
        }
        void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column != sortColumn)
            {
                sortColumn = e.Column;
                listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
            }
            else
            {
                if (listView1.Sorting == System.Windows.Forms.SortOrder.Ascending)
                    listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
                else
                    listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
            }
            this.listView1.ListViewItemSorter = new ListViewItemComparerAscDesc(e.Column);
            listView1.Sort();
            sortColumn = e.Column;
        }
        private void InitializeButtonClick()
        {
            toolStripButtonSave.Click += new EventHandler(Save);
            toolStripButtonEdit.Click += new EventHandler(Edit);
            toolStripButtonDelete.Click += new EventHandler(Delete);
            toolStripButtonClear.Click += new EventHandler(Clear);
        }
        private void setEditMode(EditMode editmode)
        {
            toolStripButtonSave.Enabled = (editmode == EditMode.New || editmode == EditMode.Update) && m_mainForm.CurrentUser.FORM_USER_SAVE ;
            toolStripButtonEdit.Enabled = (editmode == EditMode.View) && m_mainForm.CurrentUser.FORM_USER_SAVE;
            toolStripButtonDelete.Enabled = (editmode == EditMode.View) && m_mainForm.CurrentUser.FORM_USER_DELETE && !isAdmin();
            toolStripButtonClear.Enabled = m_mainForm.CurrentUser.FORM_USER_SAVE;
            ReloadMainFormButton();
        }

        private void ReloadMainFormButton()
        {
            m_mainForm.EnableButtonSave(toolStripButtonSave.Enabled);
            m_mainForm.EnableButtonEdit(toolStripButtonEdit.Enabled);
            m_mainForm.EnableButtonDelete(toolStripButtonDelete.Enabled);
            m_mainForm.EnableButtonClear(m_mainForm.CurrentUser.FORM_USER_SAVE);
        }
        private void loadRecords()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                listView1.Items.Clear();
                IList records = RepositoryFactory.GetInstance().CodeNameRepository().GetAll(m_user);
                foreach (User d in records)
                {
                    ListViewItem item = listView1.Items.Add(d.CODE);
                    item.SubItems.Add(d.NAME);
                    item.Tag = d;
                }
                this.Cursor = Cursors.Default;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void updateCurrentRecord()
        {
            foreach (ListViewItem itm in listView1.Items)
            {
                User d = (User)itm.Tag;
                if (m_user.ID == d.ID)
                {
                    itm.SubItems[0].Text = d.CODE;
                    itm.SubItems[1].Text = d.NAME;
                    return;
                }
            }
            ListViewItem item = listView1.Items.Add(m_user.CODE);
            item.SubItems.Add(m_user.NAME);
            item.Tag = m_user;
        }
        public void ClearForm()
        {
            try
            {
                textBoxCode.Text = "";//"ACC" + (RepositoryFactory.GetInstance().UserRepository().GetMaximumID(m_user) + 1).ToString().PadLeft(3, '0');
                textBoxName.Text = "";
                textBoxPassword.Text = "";
                textBoxPasswordConfirm.Text = "";
                checkBoxf_u_a.Checked = true;
                checkBoxf_u_s.Checked = true;
                checkBoxf_u_d.Checked = true;
                checkBoxf_tt_a.Checked = true;
                checkBoxf_tt_s.Checked = true;
                checkBoxf_tt_d.Checked = true;
                checkBoxf_ccy_a.Checked = true;
                checkBoxf_ccy_s.Checked = true;
                checkBoxf_ccy_d.Checked = true;
                checkBoxf_dt_a.Checked = true;
                checkBoxf_dt_s.Checked = true;
                checkBoxf_dt_d.Checked = true;
                checkBox_f_drtr_a.Checked = true;
                checkBox_f_drtr_s.Checked = true;
                checkBox_f_drtr_d.Checked = true;
                checkBox_f_crtr_a.Checked = true;
                checkBox_f_crtr_s.Checked = true;
                checkBox_f_crtr_d.Checked = true;
                checkBox_f_drtrrpt_a.Checked = true;
                checkBox_f_crtrrpt_a.Checked = true;
                checkBoxOwnerAccess.Checked = true;
                checkBoxOwnerDelete.Checked = true;
                checkBoxOwnerSave.Checked = true;
                checkBoxVesselAccess.Checked = true;
                checkBoxVesselDelete.Checked = true;
                checkBoxVesselSave.Checked = true;
                checkBoxTOPAccess.Checked = true;
                checkBoxTOPDelete.Checked = true;
                checkBoxTOPSave.Checked = true;
                m_user = new User();
                errorProvider1.Clear();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Save(object sender, EventArgs e)
        {
            try
            {
                if (Valid())
                {
                    this.Cursor = Cursors.WaitCursor;
                    UpdateEntity();
                    if (m_user.ID == 0)
                    {
                        RepositoryFactory.GetInstance().CodeNameRepository().Save(m_user);
                    }
                    else
                    {
                        RepositoryFactory.GetInstance().CodeNameRepository().Update(m_user);
                    }
                    MessageBox.Show("Record has been saved");
                    //updateCurrentRecord();
                    ClearForm();
                    loadRecords();
                    textBoxCode.Focus();
                    this.Cursor = Cursors.Default;
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void UpdateEntity()
        {
            m_user.CODE = textBoxCode.Text.Trim();
            m_user.NAME = textBoxName.Text.Trim();
            m_user.PASSWORD = textBoxPassword.Text;
            m_user.FORM_USER_ACCESS = checkBoxf_u_a.Checked;
            m_user.FORM_USER_SAVE = checkBoxf_u_s.Checked;
            m_user.FORM_USER_DELETE = checkBoxf_u_d.Checked;
            m_user.FORM_TT_ACCESS = checkBoxf_tt_a.Checked;
            m_user.FORM_TT_SAVE = checkBoxf_tt_s.Checked;
            m_user.FORM_TT_DELETE = checkBoxf_tt_d.Checked;
            m_user.FORM_CCY_ACCESS = checkBoxf_ccy_a.Checked;
            m_user.FORM_CCY_SAVE = checkBoxf_ccy_s.Checked;
            m_user.FORM_CCY_DELETE = checkBoxf_ccy_d.Checked;
            m_user.FORM_DT_ACCESS = checkBoxf_dt_a.Checked;
            m_user.FORM_DT_SAVE = checkBoxf_dt_s.Checked;
            m_user.FORM_DT_DELETE = checkBoxf_dt_d.Checked;

            m_user.FORM_DRTR_ACCESS = checkBox_f_drtr_a.Checked;
            m_user.FORM_DRTR_SAVE = checkBox_f_drtr_s.Checked;
            m_user.FORM_DRTR_DELETE = checkBox_f_drtr_d.Checked;
            m_user.FORM_CRTR_ACCESS = checkBox_f_crtr_a.Checked;
            m_user.FORM_CRTR_SAVE = checkBox_f_crtr_s.Checked;
            m_user.FORM_CRTR_DELETE = checkBox_f_crtr_d.Checked;
            m_user.FORM_DRTRRPT_ACCESS = checkBox_f_drtrrpt_a.Checked;
            m_user.FORM_CRTRRPT_ACCESS = checkBox_f_crtrrpt_a.Checked;
            m_user.FORM_OWNER_ACCESS = checkBoxOwnerAccess.Checked;
            m_user.FORM_OWNER_DELETE = checkBoxOwnerDelete.Checked;
            m_user.FORM_OWNER_SAVE = checkBoxOwnerSave.Checked;

            m_user.FORM_VESSEL_ACCESS = checkBoxVesselAccess.Checked;
            m_user.FORM_VESSEL_DELETE = checkBoxVesselDelete.Checked;
            m_user.FORM_VESSEL_SAVE = checkBoxVesselSave.Checked;

            m_user.FORM_TOP_ACCESS = checkBoxTOPAccess.Checked;
            m_user.FORM_TOP_DELETE = checkBoxTOPDelete.Checked;
            m_user.FORM_TOP_SAVE = checkBoxTOPSave.Checked;

        }
        public bool Valid()
        {
            bool a = textBoxCode.Text == "";
            bool b = textBoxName.Text == "";
            bool c = textBoxPassword.Text == "";
            bool d = textBoxPasswordConfirm.Text == "";
            bool e = textBoxPassword.Text != textBoxPasswordConfirm.Text;

            if (a) errorProvider1.SetError(textBoxCode, "Code Can not Empty");
            if (b) errorProvider1.SetError(textBoxName, "Name Can not Empty");
            if (c) errorProvider1.SetError(textBoxPassword, "Password Can not Empty");
            if (d) errorProvider1.SetError(textBoxPasswordConfirm, "Password Confirm Can not Empty");
            if (e) errorProvider1.SetError(textBoxPassword, "Please retype Password");

            return !a && !b && !c && !d && !e;
        }
        public void Edit(object sender, EventArgs e)
        {
            setEnableForm(true);
            setEditMode(EditMode.Update);
        }
        public void setEnableForm(bool enable)
        {
            textBoxCode.ReadOnly = !enable;
            if (isAdmin()) textBoxCode.ReadOnly = true;
            textBoxName.ReadOnly = !enable;
            textBoxPassword.ReadOnly = !enable;
            textBoxPasswordConfirm.ReadOnly = !enable;
            checkBoxf_u_a.Enabled = enable;
            checkBoxf_u_d.Enabled = enable;
            checkBoxf_u_s.Enabled = enable;
            checkBoxf_tt_a.Enabled = enable;
            checkBoxf_tt_d.Enabled = enable;
            checkBoxf_tt_s.Enabled = enable;
            checkBoxf_ccy_a.Enabled = enable;
            checkBoxf_ccy_d.Enabled = enable;
            checkBoxf_ccy_s.Enabled = enable;
            checkBoxf_dt_a.Enabled = enable;
            checkBoxf_dt_d.Enabled = enable;
            checkBoxf_dt_s.Enabled = enable;

            checkBox_f_drtr_a.Enabled = enable;
            checkBox_f_drtr_d.Enabled = enable;
            checkBox_f_drtr_s.Enabled = enable;
            checkBox_f_crtr_a.Enabled = enable;
            checkBox_f_crtr_d.Enabled = enable;
            checkBox_f_crtr_s.Enabled = enable;
            checkBox_f_drtrrpt_a.Enabled = enable;
            checkBox_f_crtrrpt_a.Enabled = enable;
            checkBoxTOPAccess.Enabled = enable;
            checkBoxTOPDelete.Enabled = enable;
            checkBoxTOPSave.Enabled = enable;
            checkBoxOwnerAccess.Enabled = enable;
            checkBoxOwnerDelete.Enabled = enable;
            checkBoxOwnerSave.Enabled = enable;
            checkBoxVesselAccess.Enabled = enable;
            checkBoxVesselDelete.Enabled = enable;
            checkBoxVesselSave.Enabled = enable;
        }
        public void Delete(object obj, EventArgs e)
        {
            try
            {
                if (m_user.ID > 0)
                {
                    this.Cursor = Cursors.WaitCursor;
                    if (MessageBox.Show("Please confirm to delete", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) { this.Cursor = Cursors.Default; return; }
                    RepositoryFactory.GetInstance().CodeNameRepository().Delete(m_user);
                    removeRecord(m_user.ID);
                    ClearForm();
                    setEnableForm(true);
                    setEditMode(EditMode.New);
                    this.Cursor = Cursors.Default;
                }

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Cursor = Cursors.Default;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void removeRecord(int id)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                User dep = (User)item.Tag;
                if (dep.ID == id)
                {
                    listView1.Items.Remove(item);
                    break;
                }
            }
        }
        public void Clear(object sender, EventArgs e)
        {
            ClearForm();
            setEnableForm(true);
            setEditMode(EditMode.New);
            textBoxCode.Focus();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            ClearForm();
            m_user = (User)listView1.SelectedItems[0].Tag;
            loadData();
            setEnableForm(false);
            setEditMode(EditMode.View);
        }
        private void loadData()
        {
            textBoxCode.Text = m_user.CODE;
            textBoxName.Text = m_user.NAME;
            textBoxPassword.Text = m_user.PASSWORD;
            textBoxPasswordConfirm.Text = m_user.PASSWORD;
            checkBoxf_u_a.Checked = m_user.FORM_USER_ACCESS;
            checkBoxf_u_s.Checked = m_user.FORM_USER_SAVE;
            checkBoxf_u_d.Checked = m_user.FORM_USER_DELETE;
            checkBoxf_tt_a.Checked = m_user.FORM_TT_ACCESS;
            checkBoxf_tt_s.Checked = m_user.FORM_TT_SAVE;
            checkBoxf_tt_d.Checked = m_user.FORM_TT_DELETE;
            checkBoxf_ccy_a.Checked = m_user.FORM_CCY_ACCESS;
            checkBoxf_ccy_s.Checked = m_user.FORM_CCY_SAVE;
            checkBoxf_ccy_d.Checked = m_user.FORM_CCY_DELETE;
            checkBoxf_dt_a.Checked = m_user.FORM_DT_ACCESS;
            checkBoxf_dt_s.Checked = m_user.FORM_DT_SAVE;
            checkBoxf_dt_d.Checked = m_user.FORM_DT_DELETE;

            checkBox_f_drtr_a.Checked = m_user.FORM_DRTR_ACCESS;
            checkBox_f_drtr_s.Checked = m_user.FORM_DRTR_SAVE;
            checkBox_f_drtr_d.Checked = m_user.FORM_DRTR_DELETE;
            checkBox_f_crtr_a.Checked = m_user.FORM_CRTR_ACCESS;
            checkBox_f_crtr_s.Checked = m_user.FORM_CRTR_SAVE;
            checkBox_f_crtr_d.Checked = m_user.FORM_CRTR_DELETE;

            checkBox_f_drtrrpt_a.Checked = m_user.FORM_DRTRRPT_ACCESS;
            checkBox_f_crtrrpt_a.Checked = m_user.FORM_CRTRRPT_ACCESS;

            checkBoxVesselAccess.Checked = m_user.FORM_VESSEL_ACCESS;
            checkBoxVesselDelete.Checked = m_user.FORM_VESSEL_DELETE;
            checkBoxVesselSave.Checked = m_user.FORM_VESSEL_SAVE;

            checkBoxTOPAccess.Checked = m_user.FORM_TOP_ACCESS;
            checkBoxTOPDelete.Checked = m_user.FORM_TOP_DELETE;
            checkBoxTOPSave.Checked = m_user.FORM_TOP_SAVE;

            checkBoxOwnerAccess.Checked = m_user.FORM_OWNER_ACCESS;
            checkBoxOwnerDelete.Checked = m_user.FORM_OWNER_DELETE;
            checkBoxOwnerSave.Checked = m_user.FORM_OWNER_SAVE;

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void UserForm_Activated(object sender, EventArgs e)
        {
            ReloadMainFormButton();
        }
        private bool isAdmin()
        {
            return m_user.CODE == "ADMIN";
        }

        public void Refresh(object sender, EventArgs e)
        {
            loadRecords();
        }
    }
}
